package com.heima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.heima.common.PageResult;
import com.heima.common.Result;
import com.heima.mapper.VmMapper;
import com.heima.pojo.VmType;
import com.heima.pojo.dto.VmPageDTO;
import com.heima.service.VmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class VmServiceImpl implements VmService {
    @Autowired
    private VmMapper vmMapper;


    @Override
    public PageResult pageQuery(VmPageDTO vmPageDTO) {
        PageHelper.startPage(vmPageDTO.getPageIndex(),vmPageDTO.getPageSize());
        Page<VmType> vmTypes = vmMapper.pageQuery(vmPageDTO);
        return new PageResult(vmPageDTO.getPageIndex(),vmPageDTO.getPageSize(),vmTypes.getPages() ,vmTypes.getTotal(), vmTypes.getResult());
    }

    /**
     * 新增设备类型
     */
    @Override
    public Result add(VmType vmType) {
        if(vmMapper.query(vmType) != null){
            return Result.error("重复添加");
        }
        vmMapper.insert(vmType);
        return Result.success();
    }

    /**
     * 售货机类型修改
     */
    @Override
    public void update(Integer typeId, VmType vmType) {
        vmType.setTypeId(typeId);
        vmMapper.update(vmType);
    }

    /**
     * 售货机类型删除
     */
    @Override
    public void delete(Integer typeId) {
        vmMapper.delete(typeId);
    }
}
